﻿
function Paging() {
    //初始化
    this.pageSize = 10; //分页大小
    this.pageIndex = 1; //当前页
    this.dataCount = 0;//数据条数
    this.pageCount = 10; //总页数
    this.bigPageIndex = 1; //当前大页
    this.bigPageSize = 10; //大页大小
    this.bigPageCount = 1; //大页总页数
	this.pageStart = 1;
	this.pageEnd = 1;
}

//计算页数和大页数
Paging.prototype.calculatePage = function() {
    //计算总页数
    var minpageCount = Math.floor(this.dataCount / this.pageSize);
    if (minpageCount * this.pageSize == this.dataCount) {
        this.pageCount = minpageCount;
    } else {
        this.pageCount = minpageCount + 1;
    }
    //计算大页页数
    var minBigPageCount = Math.floor(this.pageCount / this.bigPageSize);
    if (minBigPageCount * this.bigPageSize == this.pageCount) {
        this.bigPageCount = minBigPageCount;
    } else {
        this.bigPageCount = minBigPageCount + 1;
    }
};
Paging.prototype.setPageInfo = function() {
    var currentPageStart = (this.pageIndex - 1) * this.pageSize + 1;
    var currentPageEnd = (currentPageStart + this.pageSize - 1) > this.dataCount ? this.dataCount : (currentPageStart + this.pageSize - 1);
    currentPageStart = currentPageStart > currentPageEnd ? currentPageEnd : currentPageStart;
    this.pageEnd = currentPageEnd;
    this.pageStart = currentPageStart;

    return "<p>当前显示第&nbsp;<span>" + currentPageStart + "-" + currentPageEnd + "</span>&nbsp;条&nbsp;&nbsp;共<span>" + this.dataCount + "</span>条</p>";
};
Paging.prototype.prevPage = function() {
    if (this.bigPageIndex > 1)
        return "<li class=\"bh-zzg-ny-cleftbrbs2xyy\"><a id=\"prev\" href=\"javascript:void(0)\" onclick=\"paging.prev()\">上一页</a></li>";
    else {
        return "";
    }
};
Paging.prototype.nextPage = function() {
    if (this.bigPageIndex < this.bigPageCount) {
        return "<li class=\"bh-zzg-ny-cleftbrbs2xyy\"><a id=\"next\" href=\"javascript:void(0)\" onclick=\"paging.next()\">下一页</a></li>";
    } else {
        return "";
    }
};
Paging.prototype.getPageNum = function() {
    var text = "";
    for (var i = 1; i <= this.bigPageSize; i++) {
        var pageNum = (this.bigPageIndex - 1) * this.bigPageSize + i;
        if (pageNum > this.pageCount) {
            break;
        }
        if (this.pageIndex == pageNum) {
            text += "<li class=\"on\">" + pageNum + "</li>";
        }
        else {
            text += "<li><a id=\"num\" href=\"javascript:void(0)\" onclick=\"paging.gotoPage(this)\">" + pageNum + "</a></li>";
        }
    }
    return text;
};
Paging.prototype.prev = function() {
    //如果已经是第一页则不执行
    if (this.bigPageIndex == 1) {
        return;
    }
    this.bigPageIndex = this.bigPageIndex - 1;
    this.pageIndex = this.bigPageIndex * this.bigPageSize;
    this.loadPagingText();
    this.query();
};
Paging.prototype.next = function() {
    if (this.bigPageIndex >= this.bigPageCount) {
        return;
    }
    this.bigPageIndex = this.bigPageIndex + 1;
    this.pageIndex = (this.bigPageIndex - 1) * this.bigPageSize + 1;
    this.loadPagingText();
    this.query();
};
Paging.prototype.gotoPage = function(obj) {
    if ($(obj).text() > this.pageCount) {
        alert("页面索引错误！");
    }
    this.pageIndex = $(obj).text();
    this.loadPagingText();
    this.query();
};
Paging.prototype.loadPagingText = function() {
    this.calculatePage();
    var pagingHtml = "<div class=\"bh-zzg-ny-cleftbrbs1\">" + this.setPageInfo() + "</div><div class=\"bh-zzg-ny-cleftbrbs2\"><ul>" + this.prevPage() + this.getPageNum() + this.nextPage() + "<div class=\"bh-clear\"></div></ul></div></div><div class=\"bh-clear\"></div>";
    $("#paging").html(pagingHtml);
};
